package com.honey.mybatis.generator.dao.method;

import com.honey.compilation.java.AbstractJavaCompilation;
import com.honey.compilation.java.ElseBlock;
import com.honey.compilation.java.IfBlock;
import com.honey.compilation.java.Method;
import com.honey.compilation.java.MethodDecoration;
import com.honey.compilation.java.Parameter;
import com.honey.core.dbmapping.introspect.IntrospectSchema;
import com.honey.core.dbmapping.introspect.IntrospectSchemaColumn;
import com.honey.core.types.FullyQualifiedJavaType;
import com.honey.core.types.JDKFullyQualifiedJavaType;
import com.honey.mybatis.generator.AbstractMethod;
import com.honey.mybatis.generator.MybatisPlugin;
import com.honey.mybatis.generator.dao.DaoConstant;
import com.honey.mybatis.generator.entity.EntityConstant;
import com.honey.mybatis.generator.page.PageConstant;

public class BaseDaoSelectList2Method extends AbstractMethod {

	private static final FullyQualifiedJavaType RETURN_TYPE = new FullyQualifiedJavaType("java.util.List<"+EntityConstant.ENTITY_GENERIC_NAME+">"); 
	
	private static final String[] JAVA_DOC = new String[]{
		"查询列表。方法的特征如下:<br />",
		"<table  border=\"1\">",
		"  <tr>",
		"    <td align=\"center\">"+PageConstant.PAGE_VARIABLE_NAME+"的total &lt; 0</td>",
		"    <td align=\"center\">"+PageConstant.PAGE_VARIABLE_NAME+"的total = 0</td>",
		"    <td align=\"center\">"+PageConstant.PAGE_VARIABLE_NAME+"中的total &gt; 0</td>",
		"  </tr>",
		"  <tr>",
		"    <td>不使用分页查询</td>",
		"    <td>重新计算符合当前条件记录集的总数量,按照"+PageConstant.PAGE_VARIABLE_NAME+"中指定参数进行分页</td>",
		"    <td>不重新计算符合当前条件记录集的总数量,按照"+PageConstant.PAGE_VARIABLE_NAME+"中指定参数进行分页</td>",
		"  </tr>",
		"</table>",
	} ;

	/**
	 * @param clazz
	 * @param belongPlugin
	 * @param introspectSchema
	 * @param introspectSchemaColumn
	 */
	public BaseDaoSelectList2Method(AbstractJavaCompilation clazz,
			MybatisPlugin belongPlugin, IntrospectSchema introspectSchema,
			IntrospectSchemaColumn introspectSchemaColumn) {
		super(clazz, belongPlugin, introspectSchema, introspectSchemaColumn);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#getMethodName()
	 */
	@Override
	protected String getMethodName() {
		
		return DaoConstant.SELECT_LIST_METHOD_NAME;
	}

	/*
	 * (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#getParameter()
	 */
	@Override
	protected Parameter[] getParameter() {
		return new Parameter[]{
				new Parameter(DaoConstant.SQL_SESSION_PACKAGE,"session"),
				new Parameter(JDKFullyQualifiedJavaType.getStringInstance(),"statement","Mybatis映射配置。"),
				new Parameter(JDKFullyQualifiedJavaType.getStringInstance(),"statementCount","count的Mybatis映射配置。"),
				new Parameter(JDKFullyQualifiedJavaType.getObjectInstance(),"parameter","查询条件"),
				new Parameter(new FullyQualifiedJavaType(mybatisPlugin.getModelTargetPackage()+"."+PageConstant.PAGE_NAME),PageConstant.PAGE_VARIABLE_NAME,"分页"),
			};
	}

	/*
	 * (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#getJavaDoc()
	 */
	@Override
	protected String[] getJavaDoc() {
		return JAVA_DOC;
	}

	/*
	 * (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#getMethodDecoration()
	 */
	@Override
	protected MethodDecoration getMethodDecoration() {
		return super.getDependentModel().getMethodDecoration(null);
	}

	/*
	 * (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#getReturnType()
	 */
	@Override
	protected FullyQualifiedJavaType getReturnType() {
		return RETURN_TYPE;
	}

	/*
	 * (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#addBody(com.honey.compilation.java.Method)
	 */
	@Override
	protected void addBody(Method method) {
		IfBlock ifBlock = new  IfBlock(PageConstant.PAGE_VARIABLE_NAME + " == null");
		ifBlock.addBodyLine(PageConstant.PAGE_VARIABLE_NAME + " = new "+PageConstant.PAGE_NAME+"()");
		method.addBlocks(ifBlock);
		method.addBodyLine("RowBounds rowBounds = new RowBounds("+PageConstant.PAGE_VARIABLE_NAME+".getBegin(),"+PageConstant.PAGE_VARIABLE_NAME+".getInterval())");
		method.addBodyLine("List<"+EntityConstant.ENTITY_GENERIC_NAME+"> list = null");
		
		ifBlock = new  IfBlock(PageConstant.PAGE_VARIABLE_NAME+".getTotal() < 0");
		ifBlock.addBodyLine("list = session.selectList(statement, parameter)");
		method.addBlocks(ifBlock);
		ElseBlock eb = new ElseBlock();
		eb.addBodyLine("list = session.selectList(statement, parameter,rowBounds)");
		ifBlock = new  IfBlock(PageConstant.PAGE_VARIABLE_NAME+".getTotal() == 0 && list.size() > 0");
		ifBlock.addBodyLine("Integer total = (Integer)session.selectOne(statementCount, parameter)");
		ifBlock.addBodyLine(PageConstant.PAGE_VARIABLE_NAME+".setTotal(total)");
		eb.addBlocks(ifBlock);
		
		method.addBlocks(eb);
		method.addBodyLine("return list;");
	}
}
